package com.exp.controller;

import com.exp.dao.UserMapper;
import com.exp.model.User;
import com.exp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by P0015475 on 2015/9/11.
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
/*页面*/
    @RequestMapping("/view")
    public String view() {
        return "user/userList";
    }
/*获取列表*/
    @RequestMapping("/list")
    @ResponseBody
    public List<User> list(@RequestParam Map<String, Object> params) {
        List<User> users = userService.getList(params);
        return users;
    }
    /*删除*/
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public boolean delete(@PathVariable Integer id) {
        userService.delete(id);
        return true;
    }
    /*增加修改*/
    @RequestMapping("/edit")
    @ResponseBody
    public boolean edit(User user){
        try {
            if(user.getId()!=null){
                userService.update(user);
            }else{
                userService.add(user);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    /*注册用户*/
    @RequestMapping("/register")
    @ResponseBody
    public boolean register(@RequestBody User user) {
       int flag=userService.add(user);
       if(flag>0){
           return true;
       }else {
           return  false;
       }
    }
}
